<%@page import="java.io.File"%>
<%@page import="util.Configuration"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="themes/js/jquery-1.9.1.js" type="text/javascript"></script>
<style type="text/css" >@import url("themes/css/StileSettings.css"); </style>
<title>Names Management - Settings</title>
</head>
<body>
<div class="containerSettings">
	<div class="settingsTitle">
	
		<h1>Settings</h1>
	</div>
	<%	
		
	String logout= (String)request.getParameter("logout");
	if(logout!=null && logout.equals("Log Out")) session.setAttribute("login", null);
	String login=(String)session.getAttribute("login");
	System.out.println("\t"+logout+" "+login);
	if(login!=null && login.equals("logged")){ %>
	<div class="logout">
		<form action="" method="post">
			<input type="submit" id="submit" name="logout"  value="Log Out">
		</form>
	</div>
	<div class="settingsClass">
		
<div class="ResetNamesContainer">
		<b>Reset The Names Database</b> <br><br><br>
			<a href="#" class="button" title="Click if you want to Delete all the names and restart from 0 names" onclick="deleteEntireDB();">Reset entire Names DB</a>
			<br><br><br>
			<a href="#" class="button" title="Click if you want to Reset the statistics about how many objects have been processed" onclick="resetStatistics();">Reset the statistics</a>
			<br><br><br>
			<a href="#" class="button" title="Click if you want to Delete all the extra-info, like DBpedia-Character or Eclap-User info" onclick="deleteExtraInfo()">Reset extra-infos of names</a>
			<br><br><br>

		
	</div>



<script>
function deleteEntireDB(){
	var confirmation=confirm('Do you really want to delete entire names-db and the related info?');
	if (confirmation){
		//allora ha cliccato ok
		$.ajax({
		      url: "ResetDBNomiServlet",
		      type: "post",
		      async: false,
		      cache: false,
		      timeout: 30000,
		      data: {type:"eraseAllExtractedName"},
		      success: function(data,status){
		    	  if(data.indexOf("Errore nelle impostazioni! Forse non si è fatto il login, riprovare!") !== -1){
	            	  alert(data);
	              }else{
		    	  alert("Done: the server has deleted all names, related info and and wiped out the dates of the analyzed objects.")
	              console.log("Il server ha cancellato l'intero DB dei nomi"); 
	              serverOk=true;
	              }
	             
	          },
		      error:function(){
		          alert("Il server ha risposto con un errore");
		          serverOk=false;
		         
		      }   
		    });
	}
	else{
		//allora ha cliccato annulla
	}
	
}
function resetStatistics(){
	var confirmation=confirm('Do you really want to reset dates of analyzed objects?');
	if (confirmation){
		//allora ha cliccato ok
		$.ajax({
		      url: "ResetDBNomiServlet",
		      type: "post",
		      async: false,
		      cache: false,
		      timeout: 30000,
		      data: {type:"eraseNameExtractionDate"},
		      success: function(data,status){
		    	  if(data.indexOf("Errore nelle impostazioni! Forse non si è fatto il login, riprovare!") !== -1){
	            	  alert(data);
	              }else{
		    	  alert("Done: the server has wiped out the dates of the analyzed objects.")
	              console.log("Il server ha resettato le date degli oggetti analizzati"); 
		    	  
	              serverOk=true;
	              }
	          },
		      error:function(){
		          alert("Il server ha risposto con un errore");
		          serverOk=false;
		         
		      }   
		    });
	}
	else{
		//allora ha cliccato annulla
	}
	
}

function deleteExtraInfo(){
	var confirmation=confirm('Do you really want to delete the extra-infos of all objects?');
	if (confirmation){
		//allora ha cliccato ok
		$.ajax({
		      url: "ResetDBNomiServlet",
		      type: "post",
		      async: false,
		      cache: false,
		      timeout: 30000,
		      data: {type:"eraseAllExtraInfo"},
		      success: function(data,status){
		    	  if(data.indexOf("Errore nelle impostazioni! Forse non si è fatto il login, riprovare!") !== -1){
	            	  alert(data);
	              }else{
		    	  alert("Done: the server has deleted the extra-infos of all objects.")
	              console.log("Il server ha cancellato tutte le extra-info degli oggetti"); 
	              serverOk=true;
	              }
	          },
		      error:function(){
		          alert("Il server ha risposto con un errore");
		          serverOk=false;
		         
		      }   
		    });
	}
	else{
		//allora ha cliccato annulla
	}
	
}

</script>
		<%
		System.out.println("Avviata jsp(servlet) settings.jsp");
		Configuration conf=Configuration.getIstance();
		File dbConfig=conf.getDbConfigFile();
		File confprop=conf.getEclapConfigFile();
		%>
		<br>
		<div class="setPropertiesPathClass">
			<b>Set where to get the properties files</b> <br><br><br>
				<label for="setDBpropertiesPath">DB config (properties file) path:</label>
				<input type="text" id="setDBpropertiesPath" name="setDBpropertiesPath" value='<%=dbConfig==null?"":dbConfig.getCanonicalFile()%>' onkeypress="" onfocus="" onblur='' >
				<a href="#" class="buttonConfirm" title="Click to confirm path" onclick="changeDBpropertiesPath()">Ok</a>
				<br> <br>
				<label for="setConfigpropertiesPath">Synonymous config (properties file) path:</label>
				<input type="text" id="setConfigpropertiesPath" name="setConfigpropertiesPath" value='<%=confprop==null?"":confprop.getCanonicalFile()%>' onkeypress="" onfocus="" onblur='' >
				<a href="#" class="buttonConfirm" title="Click to confirm path" onclick="changeSynonPropertiesPath()">Ok</a>
				<br><br>
				<script>
				function changeDBpropertiesPath(){
					var confirmation=confirm('Do you really want to change tha db.properties path?');
					var nuovoPath=document.getElementById('setDBpropertiesPath').value;//jQuery("#setDBpropertiesPath").attr("value");
					if (confirmation){
						//allora ha cliccato ok
						$.ajax({
						      url: "SettingsServlet",
						      type: "post",
						      async: false,
						      cache: false,
						      timeout: 30000,
						      data: {type:"setDBpropertiesPath", newpath:nuovoPath},
						      success: function(data,status){
						    	  if (data==nuovoPath){
						    		  alert("Done: you have chenged the DB properties path!");
						              console.log("Il server ha aggiornato il path del fil db.properties"); 
						             
						    	  }else{
						    		  alert("Fail: something's wrong! The path is: "+ data);
						    		  jQuery("#setDBpropertiesPath").val(data);
						    	  }
						    	  serverOk=true;
					          },
						      error:function(){
						    	  alert("Fail: something's wrong!");
					    		  jQuery("#setDBpropertiesPath").val(data);
						          serverOk=false;
						         
						      }   
						    });
					}
					else{
						//allora ha cliccato annulla
					}
					
				}
				
				function changeSynonPropertiesPath(){
					var confirmation=confirm('Do you really want to change tha synonymous properties path?');
					var nuovoPath=document.getElementById('setConfigpropertiesPath').value;//jQuery("#setConfigpropertiesPath").attr("value");
					if (confirmation){
						//allora ha cliccato ok
						$.ajax({
						      url: "SettingsServlet",
						      type: "post",
						      async: false,
						      cache: false,
						      timeout: 30000,
						      data: {type:"setConfigpropertiesPath", newpath:nuovoPath},
						      success: function(data,status){
						    	  if (data==nuovoPath){
						    		  alert("Done: you have chenged the Synonymous system properties path!");
						              console.log("Il server ha aggiornato il path del file synonymous.properties"); 
						             
						    	  }else{
						    		  alert("Fail: something's wrong! The path is: "+ data);
						    		  jQuery("#setConfigpropertiesPath").val(data);
						    	  }
						    	  serverOk=true;
					          },
						      error:function(){
						    	  alert("You entered the value that was already set or something's wrong!");
					    		  jQuery("#setConfigpropertiesPath").val(data);
						          serverOk=false;
						         
						      }   
						    });
					}
					else{
						//allora ha cliccato annulla
					}
					
				}
				</script>
				
		
	</div>
	</div>
	
<%}else{
	
	String colore="color=\"black\"";


	if( request.getParameter("log")!=null && request.getParameter("log").equals("Log")  ){
		
		System.out.println("Parametri ricevuti di login:: user: "+request.getParameter("user")+", pass: "+request.getParameter("password"));
		if(Configuration.getIstance().autenticateSettingsUser(request.getParameter("user"), request.getParameter("password"))){
			session.setAttribute("login", "logged");
			response.sendRedirect("settings");
		}
		else{
		colore="color=\"red\"";
		}
	}
	
	%>

<div class="loginToSettings">
 	<div class="loginDIV">
    <h3>Please, log in!</h3>
  
	<form action="" method="post">

	<label class="grey" for="nome">User: </label>
	<br>
	<input class="textInput" type="text" name="user" id="nome" size="20"/>
	<br>
	<br>
	<label class="grey" for="cognome"> Password:</label>
	<br>
	<input class="textInput" type="password" name="password" id="password"  size="20" />
	<br><br>
	<input type="submit" id="submit" name="log"  value="Log">
	<br> 
	
	<small><a href="mailto:nesi@dsi.unifi.it">forgot your Username or Password?</a></small>
<br>
	


<%if(colore.equals("color=\"red\"")){ %>
<font <%=colore %> >

	<small>User, Password or both are wrong. Please re-enter them. </small>
</font>

<%}
else{%><br><%} %>
</form>
<br>
</div>
</div>

<%} %>
</div>

</body>
</html>